/*****************************************************************************
* Keppe Messenger (KMP) Keppe Project-Team User Messaging System
* Copyright (C) 2013-2014 Keppe Project-Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

#ifndef FRMLOGIN_H
#define FRMLOGIN_H

#include <QIcon>
#include <QImage>
#include <QStringList>
#include <QMainWindow>
#include <QCloseEvent>
#include <QListWidgetItem>
#include <QSystemTrayIcon>
#include <QSortFilterProxyModel>
#include "KPTListWidget.h"
#include "AvatarThread.h"
#include "config.h"
#ifdef UPDATE_MANAGER
#include "frmUpdate.h"
#include "UpdateThread.h"
#endif

namespace Ui {
class frmLogin;
}

class frmLogin : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit frmLogin(QWidget *parent = 0);
    void addContactItem(QString username);
    int addUserContact(QString username);
    int delUserContact(QString username);
    int sendMessage(QString username, QString message);
    int setUserPassword(QString oldpassword, QString newpassword);
    int setUserNick(QString nick);
    int setUserMessageState(QString message);
    bool customNickAvailable(QString username);
    bool isConnectedWithServer();
    bool uploadAvatar(QImage avatar);
    void removeMe(QString username);
    void setCustomNick(QString username, QString nickname);
    void serverChatTurnedOff();
    void showNotification(QString text, QString title, QString noteback="", bool updatePixmap=false, QPixmap pixmap=QPixmap());
    void updateWelcomeLabel(QString nick);
    void updateFriendRequestList(QString toAdd, QString toRemove);
    void addUserContactWithItem(QString username, QWidget *wthis);
    void setWidgetDesign();
    QStringList putStringToStringList(QString string);
    QByteArray fromMessage(QString message);
    QImage getAvatarImage(QString avatarImage);
    QIcon generateContactIcon(QImage avatar, QImage state);
    QString getTaskBarOnlineStateStringFromID(int id);
    QString getMessengerFrameworkVersion();
    QString getGuiFrameworkVersion();
    QString toAccountName(QByteArray account);
    QString toNotificationMessage(QByteArray notification);
    QString getCustomNick(QString username);
    QString getUserAvatar();
    QString getUserName();
#ifdef UPDATE_MANAGER
    void setUpdateThread(UpdateThread *ut);
    void setUpdateWindow(frmUpdate *uw);
    void enableUpdateNotification();
    void disableUpdateNotification();
#endif
    QString hostName;
    QString languagePath;
    ~frmLogin();

public slots:
    void playChatIn();
    void avatarInsert(QByteArray avatarPixmap, QString avatarImage);
    
private slots:
    void aboutKeppe();
    void aboutKMP();
    void on_doAutologin();
    void on_cmdLogin_clicked();
    void on_timerFR_tick();
    void on_timerSS_tick();
    void on_timerOA_tick();
    void on_timerLC_tick();
    void on_timerLB_tick();
    void on_lwContacts_itemDoubleClicked(QListWidgetItem *item);
    void closeEvent(QCloseEvent *event);
    void setStateOnline();
    void setStateAway();
    void setStateBusy();
    void setStateNotjust();
    void setStateShowoffline();
    void logoutUserWithRemoveAutologin();
    void logoutUser();
    void openLoginWindowNormal();
    void openLoginWindow(QSystemTrayIcon::ActivationReason reason);
    void endKM();
    void changePassword();
    void editProfile();
    void on_cmdClose_clicked();
    void on_labWelcomeNick_linkActivated(const QString &link);
    void on_lwContacts_customContextMenuRequested(const QPoint &pos);
    void openChat();
    void openConfig();
    void addContact();
    void showProfile();
    void setLanguage();
    void removeContact();
    void serverGroupChat();
    void triggerRemoveCustomNick();
    void triggerSetCustomNick();
    void on_cbStayLoggedIn_stateChanged(int arg1);
    void on_labRegister_linkActivated(const QString &link);
    void on_cmdStop_clicked();
    void on_cmdBack_clicked();
    void on_cmdRegister_clicked();
    void on_cmdRegisterCaptchaGenerateNew_clicked();
    void setAvatarWorkFalse();
    void readIncoming(QString);
    void noteBack(QString);
    void endNotification();
    void on_labDesignedLogin_mousePress(QMouseEvent *);
    void on_labDesignedLogin_mouseRelease(QMouseEvent *);
#ifdef UPDATE_MANAGER
    void checkUpdate();
#endif
    void on_lwContactsDesigned_customContextMenuRequested(const QPoint &pos);
    void on_lwContactsDesigned_itemDoubleClicked(QListWidgetItem *item);
    void on_txtContactsSearch_textEdited(const QString &arg1);
    void on_labContactsSearchReset_mouseRelease(QMouseEvent *);
    void on_labUserNameDesigned_mouseRelease(QMouseEvent *);
    void on_labUserStatusDesigned_mouseRelease(QMouseEvent *);
    void on_frStatus_mouseRelease(QMouseEvent *);

private:
    Ui::frmLogin *ui;
    void openChatWindow(QString username, QString nickname, QString userstate, QString pixmapstate, int onlinestate, QString avatarimage, bool minimized=false);
    void loginUserDataFromLines(bool showErrors=true, bool ignoreFailedConnection=false);
    void createSystemTrayIconMenu();
    void setRegisterCaptcha();
    void setupMenu();
    void setupLanguages();
    void setupMessenger(int onlineState, QString nickname);
    void destroyLanguages();
    void updateFriendRequests();
    void on_labContactsSearchReset_triggered();
    QString getIconPathForOnlineState(int id);
    QString getIconPathForTaskBarOnlineState(int id);
    QString getPixmapPathForNotificationOnlineState(int id);
    AvatarThread *at;
#ifdef UPDATE_MANAGER
    UpdateThread *ut;
    frmUpdate *uw;
#endif
    bool bigIcon;
    bool designedMode;
    bool mouseOverLogin;
    bool mouseOverStatus;
    bool isLoggedIn;
    bool isPhonon;
    bool isSayIC;
    bool isSWW;
    bool arWrk;
    int ustate;
    KMsgFw111Q *fw;
    QString captchaCode;
    QString myNickname;
    QStringList frList;
    QStringList arList;
    QImage defaultAvatar;
    QSystemTrayIcon *ticon;
    KPTListWidget *cContactList;
    QMap<QString,QListWidgetItem*> stringLinkContact;
    QMap<QListWidgetItem*,QString> contactLinkString;
    QMap<int,QImage> stateLinkImage;
    QMap<QString,bool> stringLinkBool;
    QMap<QString,QObject*> stringLinkChat;
    QMap<QString,QString> stringLinkNick;
    QMap<QString,QImage> stringLinkImage;
    QMap<QString,QString> stringLinkAvatar;
    QMap<QString,QString> stringLinkPixmapState;
    QMap<QString,QString> stringLinkMessageState;
    QMap<QString,QAction*> stringLinkAction;
    QMap<QString,QString> langWrapper;
    QMap<QString,int> stringLinkOnlineState;
};

#endif // FRMLOGIN_H
